app.controller('topicManage', ['$scope','$http','$rootScope', '$state','$sce','$document','$sessionStorage',function($scope,$http,$rootScope,$state,$sce,$document,$sessionStorage){
	$http({
	    url:$rootScope.baseUrl+"Exercises/getSelectList",
	    method:"post",
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.selectList=res.data.data;

	    	$scope.nowTheme=$scope.selectList.themeArr[1];//试题分类
	    	$scope.nowChapter=$scope.selectList.chapterArr[1];//试题章节

	    	$scope.getList();
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});

	// 试题题型
	$scope.topicType=["单选题","多选题","判断题"];
	$scope.nowTopic=$scope.topicType[0];

	// 选择试题题型
	$scope.selectTopicType=function(item){
		$scope.nowTopic=item;
		$scope.getList();
	}

	// 选择试题分类
	$scope.selectTheme=function(item){
		$scope.nowTheme=item;
		$scope.getList();
	}

	// 选择试题章节
	$scope.selectChapter=function(item){
		$scope.nowChapter=item;
		$scope.getList();
	}

	$scope.getList=function(){
		if($scope.nowTopic=="单选题"){
			var type=1;
		}else if($scope.nowTopic=="多选题"){
			var type=2;
		}else{
			var type=3;
		}
		var data={
			page:1,
			page_size:15,
			type:type,
			theme_id:$scope.nowTheme.id,
			chapter_id:$scope.nowChapter.id
		}
		
		if($scope.searchVal!=undefined){
			if($scope.searchVal.length!=0){
				data.search_val=$scope.searchVal;
			}
		}

		$http({
		    url:$rootScope.baseUrl+"Exercises/getList",
		    method:"get",
		    params:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.listTopic=res.data.data.list;

		    	$.each($scope.listTopic,function(index,item){
		    		item.title=$sce.trustAsHtml(item.title);
		    	});

		    	if($scope.listTopic.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(15,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                method:"post",
		    	                url:$rootScope.baseUrl+'Exercises/getList',
		    	                data:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	if($scope.listTopic.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}

	// 删除试题
	$scope.deleteTopic=function(item){
		swal({
		    title: "确定删除？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
		    $http({
		        url:$rootScope.baseUrl+"Exercises/deleteExerc",
		        method:"post",
		        data:{
		        	id:item.id
		        }
		    }).then(function(res){
		        if(res.data.code==200){
		        	swal("删除成功", "", "success");
		        	$scope.getList();
		        }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
		    },function(error){
		        console.log(error);
		    });
		});
	}

	$scope.editTopic=function(item){
		$state.go("fullScore.editExam");
		$sessionStorage.topicID=item.id;
	}
}]);